
#include <Archaism/Timer.h>
#include <windows.h>

_BeginNamespace(Radix)

struct TimerAttribute
{
	double mLastCounter;
};

Timer::Timer()
	:mAttribute(new TimerAttribute())
{
	this->reset();
}

Timer::~Timer()
{
	delete mAttribute;
}

void Timer::reset()
{
	LARGE_INTEGER counter;
	QueryPerformanceCounter(&counter);
	mAttribute->mLastCounter = (double)(counter.QuadPart);
}

longlong Timer::elapsed(bool isReset)
{
	LARGE_INTEGER counter;
	QueryPerformanceCounter(&counter);
	double curCounter = (double)(counter.QuadPart);
	double lastCounter = mAttribute->mLastCounter;
	if(isReset)
	{
		mAttribute->mLastCounter = curCounter;
	}

	LARGE_INTEGER frequency;
	QueryPerformanceFrequency(&frequency);
	double curFrequency = (double)(frequency.QuadPart);

	longlong result = (longlong)((curCounter - lastCounter) / curFrequency * 1000000);
	return result;
}

_EndNamespace(Radix)
